package com.shuwen.gcdj.controller.role;

import com.shuwen.gcdj.bean.role.request.QueryPageReq;
import com.shuwen.gcdj.common.api.CommonPage;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.bean.AddResp;
import com.shuwen.gcdj.bean.VoidResp;
import com.shuwen.gcdj.bean.role.request.AddRoleReq;
import com.shuwen.gcdj.bean.role.request.GetAllRoleReq;
import com.shuwen.gcdj.bean.role.request.UpdateRoleReq;
import com.shuwen.gcdj.bean.role.response.RoleItemResp;
import com.shuwen.gcdj.common.annotation.JWTAuth;
import com.shuwen.gcdj.common.constant.Constants;
import com.shuwen.gcdj.common.enums.LogModuleEnum;
import com.shuwen.gcdj.common.enums.LogTypeEnum;
import com.shuwen.gcdj.common.exception.ApiException;
import com.shuwen.gcdj.common.sysLog.AddSysLogUtil;
import com.shuwen.gcdj.controller.AuthController;
import com.shuwen.gcdj.dto.account.InitUserDto;
import com.shuwen.gcdj.service.account.AccountRoleService;
import com.shuwen.gcdj.service.role.RoleService;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "用户中心 - 角色管理")
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController extends AuthController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private AddSysLogUtil addSysLogUtil;

    @Autowired
    private AccountRoleService accountRoleService;

    @ApiOperation(value = "分页获取所有角色")
    @PostMapping(value = "/getAllList")
    @ResponseBody
    @JWTAuth
    public CommonResult<CommonPage<RoleItemResp>> getAllList(HttpServletRequest request,@RequestBody QueryPageReq page) {
            InitUserDto initUserDto = initUser(request);
            GetAllRoleReq req = new GetAllRoleReq();
            req.setRoleCode(page.getRoleCode());
            req.setRoleName(page.getRoleName());
            req.setRoleStatus(page.getRoleStatus());
            req.setIsHidden(Lists.newArrayList(Constants.IS_HIDDEN_NORMAL));
            if (initUserDto != null && initUserDto.getUserId() != null && roleService.isSupper(initUserDto.getRoleIds())) {
                req.setIsHidden(Lists.newArrayList(Constants.IS_HIDDEN_NORMAL, Constants.IS_HIDDEN_YES));
            }

            return CommonResult.success(CommonPage.restPage(roleService.getAllList(req,page)));

    }

    @ApiOperation(value = "获取所有角色")
    @PostMapping(value = "/getRoleList")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<RoleItemResp>> getRoleList(HttpServletRequest request) {
        List<RoleItemResp> roleList = roleService.getRoleList();
        return CommonResult.success(roleList);

    }

    @ApiOperation(value = "根据用户ID获取所有角色")
    @GetMapping(value = "/getbyaccountidAllList/{accountId}")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<RoleItemResp>> getbyaccountidAllList(HttpServletRequest request,@PathVariable String accountId) {
            if(StringUtils.isEmpty(accountId)){
                return CommonResult.failed("accountId不可为空");
            }
            InitUserDto initUserDto = initUser(request);
            log.info("initUserDto = {}", initUserDto);
            GetAllRoleReq req = new GetAllRoleReq();
            req.setIsHidden(Lists.newArrayList(Constants.IS_HIDDEN_NORMAL));
            if (initUserDto != null && initUserDto.getUserId() != null && roleService.isSupper(initUserDto.getRoleIds())) {
                req.setIsHidden(Lists.newArrayList(Constants.IS_HIDDEN_NORMAL, Constants.IS_HIDDEN_YES));
            }

            return CommonResult.success(roleService.getbyroleidAllList(req,accountId));

    }

    @ApiOperation(value = "根据角色ID获取角色详情")
    @GetMapping(value = "/getbyroleid/{roleId}")
    @ResponseBody
    @JWTAuth
    public CommonResult<RoleItemResp> getbyroleid(HttpServletRequest request,@PathVariable String roleId) {
            if(StringUtils.isEmpty(roleId)){
                return CommonResult.failed("roleId不可为空");
            }
            InitUserDto initUserDto = initUser(request);
            log.info("initUserDto = {}", initUserDto);
            GetAllRoleReq req = new GetAllRoleReq();
            req.setIsHidden(Lists.newArrayList(Constants.IS_HIDDEN_NORMAL));
            if (initUserDto != null && initUserDto.getUserId() != null && roleService.isSupper(initUserDto.getRoleIds())) {
                req.setIsHidden(Lists.newArrayList(Constants.IS_HIDDEN_NORMAL, Constants.IS_HIDDEN_YES));
            }

            return CommonResult.success(roleService.getByroleId(req,roleId));

    }

    @ApiOperation(value = "添加角色")
    @PostMapping(value = "/add")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<AddResp> add(HttpServletRequest request, @Valid @RequestBody AddRoleReq req) {
        AddResp resp = roleService.createRole(req, initUser(request));
        return CommonResult.success(resp);

    }

    @ApiOperation(value = "修改角色")
    @PostMapping(value = "/update")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> update(HttpServletRequest request, @Valid @RequestBody UpdateRoleReq req) {
        InitUserDto initUserDto = initUser(request);
        roleService.updateRole(req, initUserDto);
        CommonResult<VoidResp> resp = CommonResult.success(new VoidResp());
        addSysLogUtil.addLog(initUserDto, LogTypeEnum.LOG_TYPE_UPDATE.getTypeNum(), LogModuleEnum.LOG_MODULE_ROLE.getModuleNum(), "修改了角色",req);
        return resp;
    }

    /**
     * 删除角色
     *
     * @param roleId String
     * @return CommonResult<VoidResp>
     */
    @ApiOperation(value = "批量删除角色")
    @GetMapping(value = "/delete/{roleId}")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> delete(HttpServletRequest request, @Valid @PathVariable String roleId) {
        if(StringUtils.isEmpty(roleId)){
            throw new ApiException("roleId不能为空");
        }
        InitUserDto initUserDto = initUser(request);
        roleService.deleteRole(roleId, initUserDto);

        return CommonResult.success(new VoidResp());
    }

}
